package messages;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import client.ClientData;

import others.Constants;


public class AllClientsMessage extends Message {

	private List<String> clientsConnected = new ArrayList<String>();;

	public AllClientsMessage(List<ClientData> clientsList) {

		super("CLIENTS");

		for (Iterator<ClientData> iterator = clientsList.iterator(); iterator
				.hasNext();) {
			ClientData clientList = (ClientData) iterator.next();
			clientsConnected.add(clientList.getClientName());
		}
	}

	public AllClientsMessage(String type,List<StringTokenizer> tokens) {

		super("CLIENTS");
		parseMessage(tokens);
	}

	// retorna clientsConnected
	public List<String> getClientList() {
		return clientsConnected;
	}

	@Override
	public void parseMessage(List<StringTokenizer> tokens) {

		clientsConnected.clear();
		for (Iterator<StringTokenizer> iterator = tokens.iterator(); iterator.hasNext();) {
			StringTokenizer stringTokenizer = (StringTokenizer) iterator.next();
			if(stringTokenizer.hasMoreTokens()) {
				String client = stringTokenizer.nextToken();
				if(!client.equals("CLIENTS")) {
					clientsConnected.add(client);
				}
			}
		}
	}

	@Override
	public String writeMessage() {

		String message = "";

		message += "CLIENTS " + Constants.CRLF;
		for (Iterator<String> iterator = clientsConnected.iterator(); iterator.hasNext();) {
			String client = (String) iterator.next();
			message += client + Constants.CRLF;
		}

		return message;
	}

	// sobreescrevendo o toString
	@Override
	public String toString() {
		return "MessClients [clientsConnected=" + clientsConnected + "]";
	}

}
